package com.jbinfo.qingcaowan.exam.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.jbinfo.qingcaowan.common.CheckParamUtil;
import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.exam.dao.ChooseAnswerDao;
import com.jbinfo.qingcaowan.exam.dao.ExamUserDao;
import com.jbinfo.qingcaowan.exam.dao.QuestionDao;
import com.jbinfo.qingcaowan.exam.dto.ExamQuestionDTO;
import com.jbinfo.qingcaowan.exam.dto.ExamUserDTO;
import com.jbinfo.qingcaowan.exam.dto.QuestionDTO;
import com.jbinfo.qingcaowan.exam.entity.ExamChooseAnswer;
import com.jbinfo.qingcaowan.exam.entity.ExamQuestion;
import com.jbinfo.qingcaowan.exam.entity.ExamUserDomain;
import com.jbinfo.qingcaowan.exam.service.QuestionService;

@Service("questionService")
public class QuestionServiceImpl implements QuestionService {
	
	@Resource(name="questionDao")
	private QuestionDao questionDao;
	
	@Resource(name="chooseAnswerDao")
	private ChooseAnswerDao chooseAnswerDao;
	
	@Resource(name="examUserDao")
	private ExamUserDao examUserDao;
	
	// 根据试卷编码查询试题
	@Override
	public Object findQuestionInfo(ExamQuestionDTO queryDTO) {
		
		if (queryDTO.getExamOrder() == null 
				|| queryDTO.getExamOrder().toString().equals("")) {
			queryDTO.setExamOrder(Integer.valueOf(1));
		}
		
		CheckParamUtil.checkFieldIfNotBlank(queryDTO, "userId", "examCircle");
		
		String examCircle = queryDTO.getExamCircle();
		Integer nQuestionCnt = questionDao.findQuestionCntByCode(examCircle);
		
		ExamQuestion question = questionDao.selectQuestionInfo(queryDTO);
		
		ExamQuestionDTO dto = new ExamQuestionDTO();
		
		if (!StringUtils.isEmpty(question)) {
			
			String questionId = question.getId();
			
			dto = ClonePojoUtil.copyClassFromTo(question, ExamQuestionDTO.class);
			
			// 传出 用户选择答案及该题得分
			// 根据questionId、userId 查询用户答过此题的得分及答案
			ExamUserDTO examUserDTO = new ExamUserDTO();
			examUserDTO.setUserId(queryDTO.getUserId());
			examUserDTO.setQuestionId(questionId);
			
			List<ExamUserDomain> domainList = examUserDao.findExamUser(examUserDTO);
			
			// 考试答题状态
//			String examUserStatus = "0";
			// 判断非空
			if (domainList != null && !domainList.isEmpty()) {
				ExamUserDomain domain = domainList.get(0);
				
				List<String> userAnswerList = new ArrayList<String>();
				// 如果是多选题，则userAnswer 为多个值
				if (dto.getType().equals("1")) {
					if (domain.getUserAnswer().contains(",")) {
						String [] userAnswers = domain.getUserAnswer().split(",");
						userAnswerList = Arrays.asList(userAnswers);
					} else {
						userAnswerList.add(domain.getUserAnswer());
					}
				} else {
					userAnswerList.add(domain.getUserAnswer());
				}
				dto.setUserAnswerList(userAnswerList);
				dto.setUserScore(domain.getUserScore());
				
//				examUserStatus = "1";
			}
//			dto.setExamUserStatus(examUserStatus);
			
			List<ExamChooseAnswer> answerList = chooseAnswerDao.selectChooseAnswerById(questionId);
			
			dto.setChooseAnswerList(answerList);
		}
		return new QuestionDTO(dto, nQuestionCnt);
	}

}
